﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class BuildUIWindow : EditorWindow
{
    static BuildUIWindow window;

    Vector2 m_uiScrollView = Vector2.zero;
    Vector2 m_resScrollView = Vector2.zero;
    Vector2 m_luaScrollView = Vector2.zero;
    public static string OutputDirRoot
    {
        get
        {
            if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
                return "../Resources/android";
            else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
                return "../Resources/ios";
            else
                return "../Resources/win32";
        }
    }

    public static string TempOutDir
    {
        get
        {
            if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
                return "BundlesTemp/android";
            else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
                return "BundlesTemp/ios";
            else
                return "BundlesTemp/win32";
        }
    }

    public static string strUICommonDir = null;
    bool m_bForceBuild = false;
    bool m_bSplitAlpha = false;

    Dictionary<string, string> m_dicUIFoleder = new Dictionary<string, string>();
    Dictionary<string, string> m_dicResFolder = new Dictionary<string, string>();
    Dictionary<string, string> m_dicLuaFoleder = new Dictionary<string, string>();

    [MenuItem("Tool/Build Asset")]
    static void Execute()
    {
        EditorCommon.ClearConsole();
        window = EditorWindow.GetWindow<BuildUIWindow>();
        window.titleContent = new UnityEngine.GUIContent("Build AssetBundle");
        window.position = new Rect((Screen.currentResolution.width - 640) / 2, (Screen.currentResolution.height - 480) / 2, 640, 480);
        window.Init();
    }
    void Init()
    {
        BuildHelper.InitDicFolders(ref m_dicUIFoleder, ref m_dicResFolder, ref m_dicLuaFoleder, ref strUICommonDir);
    }

    void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        m_bForceBuild = GUILayout.Toggle(m_bForceBuild, "ForceRebuild(apkCheck)");
        m_bSplitAlpha = GUILayout.Toggle(m_bSplitAlpha, "UISplitAlpha(apkCheck)");
        EditorGUILayout.EndHorizontal();
        #region ui
        GUILayout.Label("UI");

        Dictionary<string, string>.Enumerator dicUIFolder = m_dicUIFoleder.GetEnumerator();
        bool bUIMove = dicUIFolder.MoveNext();

        m_uiScrollView = GUILayout.BeginScrollView(m_uiScrollView,true,true);

        while(bUIMove)
        {
            EditorGUILayout.BeginHorizontal();
            int index = 1;
            while (index <= 5 && bUIMove)
            {
                if (GUILayout.Button(dicUIFolder.Current.Key, GUILayout.Height(30)))
                {
                    BuildHelper.ClearAssetBundleName();
                    BuildHelper.SplitAlpha = m_bSplitAlpha;
                    BuildHelper.BuildUIAsset(strUICommonDir, OutputDirRoot, TempOutDir, dicUIFolder.Current.Key, dicUIFolder.Current.Value, m_bForceBuild);
                    BuildHelper.ClearAssetBundleName();
                }
                index++;
                bUIMove = dicUIFolder.MoveNext();
            }
            EditorGUILayout.EndHorizontal();
        }
        GUILayout.EndScrollView();

        #endregion

        #region res

        GUILayout.Label("Res");

        Dictionary<string, string>.Enumerator dicResFolder = m_dicResFolder.GetEnumerator();
        bool bResMove = dicResFolder.MoveNext();

        m_resScrollView = GUILayout.BeginScrollView(m_resScrollView, true, true);

        while (bResMove)
        {
            EditorGUILayout.BeginHorizontal();
            int index = 1;
            while (index <= 5 && bResMove)
            {
                if (GUILayout.Button(dicResFolder.Current.Key, GUILayout.Height(30)))
                {
                    BuildHelper.ClearAssetBundleName();
                    BuildHelper.BuildPackRes(OutputDirRoot, TempOutDir, dicResFolder.Current.Key, dicResFolder.Current.Value, m_bForceBuild);
                    BuildHelper.ClearAssetBundleName();
                }
                index++;
                bResMove = dicResFolder.MoveNext();
            }
            EditorGUILayout.EndHorizontal();
        }
        GUILayout.EndScrollView();

        #endregion

        #region lua

        GUILayout.Label("Lua");

        Dictionary<string, string>.Enumerator dicLuaFolder = m_dicLuaFoleder.GetEnumerator();
        bool bLuaMove = dicLuaFolder.MoveNext();

        m_resScrollView = GUILayout.BeginScrollView(m_resScrollView, true, true);

        while (bLuaMove)
        {
            EditorGUILayout.BeginHorizontal();
            int index = 1;
            while (index <= 5 && bLuaMove)
            {
                if (GUILayout.Button(dicLuaFolder.Current.Key, GUILayout.Height(30)))
                {
                    BuildHelper.ClearAssetBundleName();
                    BuildHelper.BuildLuaFiles(OutputDirRoot,TempOutDir,dicLuaFolder.Current.Key,dicLuaFolder.Current.Value);
                    BuildHelper.ClearAssetBundleName();
                }
                index++;
                bLuaMove = dicLuaFolder.MoveNext();
            }
            EditorGUILayout.EndHorizontal();
        }
        GUILayout.EndScrollView();


        #endregion


    }

}
